package net.gxf.nehe.learn;

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.ByteOrder;

import java.nio.FloatBuffer;

import org.lwjgl.input.Keyboard;

import org.lwjgl.opengl.Display;

import org.lwjgl.opengl.DisplayMode;

import org.lwjgl.opengl.GL11;

import org.lwjgl.util.glu.GLU;

import org.newdawn.slick.opengl.Texture;

import org.newdawn.slick.opengl.TextureLoader;

import org.newdawn.slick.util.ResourceLoader;

public class Lesson07 {
	/* 42 */private boolean done = false;
	/* 43 */private boolean fullscreen = false;
	/* 44 */private final String windowTitle = "NeHe's OpenGL Lesson 7 for LWJGL (Texture Filters, Lighting & Keyboard Control)";
	/* 45 */private boolean f1 = false;
	private DisplayMode displayMode;
	private boolean light;
	private boolean lp;
	private boolean fp;
	private float xrot;
	private float yrot;
	private float xspeed;
	private float yspeed;
	/* 55 */private float z = -5.0F;
	/* 56 */private float[] lightAmbient = { 0.5F, 0.5F, 0.5F, 1.0F };
	/* 57 */private float[] lightDiffuse = { 1.0F, 1.0F, 1.0F, 1.0F };
	/* 58 */private float[] lightPosition = { 0.0F, 0.0F, 2.0F, 1.0F };
	private int filter;
	private Texture texture;
	/* 61 */private boolean[] keys = new boolean[9];

	public static void main(String[] args) {
		/* 64 */System.setProperty("org.lwjgl.librarypath", System.getProperty("user.dir") + "/native/windows");
		/* 65 */boolean fullscreen = false;
		/* 66 */if ((args.length > 0) &&
		/* 67 */(args[0].equalsIgnoreCase("fullscreen"))) {
			/* 68 */fullscreen = true;
		}

		/* 72 */Lesson07 l7 = new Lesson07();
		/* 73 */l7.run(fullscreen);
	}

	public void run(boolean fullscreen) {
		/* 76 */this.fullscreen = fullscreen;
		try {
			/* 78 */init();
			/* 79 */while (!this.done) {
				/* 80 */mainloop();
				/* 81 */render();
				/* 82 */Display.update();
			}
			/* 84 */cleanup();
		} catch (Exception e) {
			/* 87 */e.printStackTrace();
			/* 88 */System.exit(0);
		}
	}

	/* 92 */private void mainloop() {
		if (Keyboard.isKeyDown(1)) {
			/* 93 */this.done = true;
		}
		/* 95 */if (Display.isCloseRequested()) {
			/* 96 */this.done = true;
		}
		/* 98 */if ((Keyboard.isKeyDown(59)) && (!this.f1)) {
			/* 99 */this.f1 = true;
			/* 100 */switchMode();
		}
		/* 102 */if (!Keyboard.isKeyDown(59)) {
			/* 103 */this.f1 = false;
		}
		/* 105 */if ((Keyboard.isKeyDown(38)) && (!this.lp)) {
			/* 106 */this.lp = true;
			/* 107 */this.light = (!this.light);
			/* 108 */if (!this.light) {
				/* 110 */GL11.glDisable(2896);
			} else {
				/* 114 */GL11.glEnable(2896);
			}
		}
		/* 117 */else if (!Keyboard.isKeyDown(38)) {
			/* 118 */this.lp = false;
		}
		/* 120 */if ((Keyboard.isKeyDown(33)) && (!this.fp)) {
			/* 121 */this.fp = true;
			/* 122 */this.filter += 1;
			/* 123 */if (this.filter > 2) {
				/* 125 */this.filter = 0;
			}
		}
		/* 128 */else if (!Keyboard.isKeyDown(33)) {
			/* 129 */this.fp = false;
		}
		/* 131 */if (Keyboard.isKeyDown(201)) {
			/* 132 */this.z -= 0.02F;
		}
		/* 134 */if (Keyboard.isKeyDown(209)) {
			/* 135 */this.z += 0.02F;
		}
		/* 137 */if (Keyboard.isKeyDown(200)) {
			/* 138 */this.xspeed -= 0.01F;
		}
		/* 140 */if (Keyboard.isKeyDown(208)) {
			/* 141 */this.xspeed += 0.01F;
		}
		/* 143 */if (Keyboard.isKeyDown(205)) {
			/* 144 */this.yspeed += 0.01F;
		}
		/* 146 */if (Keyboard.isKeyDown(203))
			/* 147 */this.yspeed -= 0.01F;
	}

	private void switchMode() {
		/* 151 */this.fullscreen = (!this.fullscreen);
		try {
			/* 153 */Display.setFullscreen(this.fullscreen);
		} catch (Exception e) {
			/* 156 */e.printStackTrace();
		}
	}

	private boolean render() {
		/* 161 */GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

		/* 163 */GL11.glLoadIdentity();

		/* 165 */GL11.glTranslatef(0.0F, 0.0F, this.z);

		/* 167 */GL11.glRotatef(this.xrot, 1.0F, 0.0F, 0.0F);
		/* 168 */GL11.glRotatef(this.yrot, 0.0F, 1.0F, 0.0F);
		/* 169 */GL11.glBindTexture(3553, this.texture.getTextureID());

		/* 171 */GL11.glBegin(7);

		/* 173 */GL11.glNormal3f(0.0F, 0.0F, 1.0F);
		/* 174 */GL11.glTexCoord2f(0.0F, 0.0F);
		GL11.glVertex3f(-1.0F, -1.0F, 1.0F);
		/* 175 */GL11.glTexCoord2f(1.0F, 0.0F);
		GL11.glVertex3f(1.0F, -1.0F, 1.0F);
		/* 176 */GL11.glTexCoord2f(1.0F, 1.0F);
		GL11.glVertex3f(1.0F, 1.0F, 1.0F);
		/* 177 */GL11.glTexCoord2f(0.0F, 1.0F);
		GL11.glVertex3f(-1.0F, 1.0F, 1.0F);

		/* 179 */GL11.glNormal3f(0.0F, 0.0F, -1.0F);
		/* 180 */GL11.glTexCoord2f(1.0F, 0.0F);
		GL11.glVertex3f(-1.0F, -1.0F, -1.0F);
		/* 181 */GL11.glTexCoord2f(1.0F, 1.0F);
		GL11.glVertex3f(-1.0F, 1.0F, -1.0F);
		/* 182 */GL11.glTexCoord2f(0.0F, 1.0F);
		GL11.glVertex3f(1.0F, 1.0F, -1.0F);
		/* 183 */GL11.glTexCoord2f(0.0F, 0.0F);
		GL11.glVertex3f(1.0F, -1.0F, -1.0F);

		/* 185 */GL11.glNormal3f(0.0F, 1.0F, 0.0F);
		/* 186 */GL11.glTexCoord2f(0.0F, 1.0F);
		GL11.glVertex3f(-1.0F, 1.0F, -1.0F);
		/* 187 */GL11.glTexCoord2f(0.0F, 0.0F);
		GL11.glVertex3f(-1.0F, 1.0F, 1.0F);
		/* 188 */GL11.glTexCoord2f(1.0F, 0.0F);
		GL11.glVertex3f(1.0F, 1.0F, 1.0F);
		/* 189 */GL11.glTexCoord2f(1.0F, 1.0F);
		GL11.glVertex3f(1.0F, 1.0F, -1.0F);

		/* 191 */GL11.glNormal3f(0.0F, -1.0F, 0.0F);
		/* 192 */GL11.glTexCoord2f(1.0F, 1.0F);
		GL11.glVertex3f(-1.0F, -1.0F, -1.0F);
		/* 193 */GL11.glTexCoord2f(0.0F, 1.0F);
		GL11.glVertex3f(1.0F, -1.0F, -1.0F);
		/* 194 */GL11.glTexCoord2f(0.0F, 0.0F);
		GL11.glVertex3f(1.0F, -1.0F, 1.0F);
		/* 195 */GL11.glTexCoord2f(1.0F, 0.0F);
		GL11.glVertex3f(-1.0F, -1.0F, 1.0F);

		/* 197 */GL11.glNormal3f(1.0F, 0.0F, 0.0F);
		/* 198 */GL11.glTexCoord2f(1.0F, 0.0F);
		GL11.glVertex3f(1.0F, -1.0F, -1.0F);
		/* 199 */GL11.glTexCoord2f(1.0F, 1.0F);
		GL11.glVertex3f(1.0F, 1.0F, -1.0F);
		/* 200 */GL11.glTexCoord2f(0.0F, 1.0F);
		GL11.glVertex3f(1.0F, 1.0F, 1.0F);
		/* 201 */GL11.glTexCoord2f(0.0F, 0.0F);
		GL11.glVertex3f(1.0F, -1.0F, 1.0F);

		/* 203 */GL11.glNormal3f(-1.0F, 0.0F, 0.0F);
		/* 204 */GL11.glTexCoord2f(0.0F, 0.0F);
		GL11.glVertex3f(-1.0F, -1.0F, -1.0F);
		/* 205 */GL11.glTexCoord2f(1.0F, 0.0F);
		GL11.glVertex3f(-1.0F, -1.0F, 1.0F);
		/* 206 */GL11.glTexCoord2f(1.0F, 1.0F);
		GL11.glVertex3f(-1.0F, 1.0F, 1.0F);
		/* 207 */GL11.glTexCoord2f(0.0F, 1.0F);
		GL11.glVertex3f(-1.0F, 1.0F, -1.0F);
		/* 208 */GL11.glEnd();

		/* 210 */this.xrot += this.xspeed;
		/* 211 */this.yrot += this.yspeed;
		/* 212 */return true;
	}

	private void createWindow() throws Exception {
		Display.setFullscreen(this.fullscreen);
		DisplayMode[] d = Display.getAvailableDisplayModes();
		for (int i = 0; i < d.length; i++) {
			if ((d[i].getWidth() == 640) && (d[i].getHeight() == 480) && (d[i].getBitsPerPixel() == 32)) {
				this.displayMode = d[i];
				break;
			}
		}
		/* 225 */Display.setDisplayMode(this.displayMode);
		/* 226 */Display.setTitle("NeHe's OpenGL Lesson 7 for LWJGL (Texture Filters, Lighting & Keyboard Control)");
		/* 227 */Display.create();
	}

	private void init() throws Exception {
		/* 230 */createWindow();

		/* 232 */loadTextures();
		/* 233 */initGL();
	}

	private void loadTextures() throws IOException {
		/* 236 */this.texture = loadTexture("Data/Crate.bmp");
		/* 236 */// this.texture = loadTexture("Data/psu.jpeg");
	}

	private void initGL() {
		GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
		GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
		GL11.glClearDepth(1.0f); // Depth Buffer Setup
		GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
		GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

		GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
		/* 247 */GL11.glLoadIdentity();

		/* 250 */GLU.gluPerspective(45.0F, this.displayMode.getWidth() / this.displayMode.getWidth(), 0.1F, 100.0F);
		/* 251 */GL11.glMatrixMode(5888);

		/* 254 */GL11.glHint(3152, 4354);
		/* 255 */ByteBuffer temp = ByteBuffer.allocateDirect(16);
		/* 256 */temp.order(ByteOrder.nativeOrder());
		GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer) temp.asFloatBuffer().put(lightAmbient).flip()); // Setup
		GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer) temp.asFloatBuffer().put(lightDiffuse).flip()); // Setup
		GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, (FloatBuffer) temp.asFloatBuffer().put(lightPosition).flip()); // Position
		GL11.glEnable(GL11.GL_LIGHT1); // Enable Light One
	}

	private void cleanup() {
		Display.destroy();
	}

	private Texture loadTexture(String path) throws IOException {
		this.texture = TextureLoader.getTexture("BMP", ResourceLoader.getResourceAsStream(path));

		return this.texture;
	}

}

/*
 * Location: C:\DBDownload\lwjglDemo2\lwjglDemo2\Run.jar Qualified Name:
 * com.nehe.learn.Lesson07 JD-Core Version: 0.6.2
 */